<?php

class DBReader extends DataReader
{
    public static $db_name = DEFAULT_DB_NAME;
    /**
     * @param string $db
     * @return OciPDO
     */
    public function db($db = null)
    {
        if (! $db)
        {
		    $backtrace = debug_backtrace();
            $caller = $backtrace[1]['class'];

            if (class_exists($caller))
            {
                $tmp_vars = get_class_vars($caller);
                if (isset($tmp_vars['app']) && $tmp_vars['app'])
                {
                    $db = $tmp_vars['app'];
                }
            }
            if (! $db)
            {
                $db = self::$db_name;
            }
        }
        
        return DBFactory::getInstance($db);
    }

    public static function getInstance($model)
    {
        static $instances = array();

        $inc_model = strtolower($model);

        if (! isset($instances[$inc_model]))
        {
            $classname = str_replace(' ', '', ucwords($model.'DB'));

            if (file_exists(MODEL_PATH . 'db' . DS . $classname . '.php'))
            {
                include_once MODEL_PATH . 'db' . DS . $classname . '.php';
            }

            $instances[$model] = new $classname();
        }
        return $instances[$inc_model];
    }

    public function pager($curr_page = 1, $per_page = 10, $ct_db_name = DEFAULT_DB_NAME, $select_db_name = '')
    {
        include_once COMMON_LIB_PATH . 'Pager.class.php';
        if (! $select_db_name)
        {
            $select_db_name = $ct_db_name;
        }
        return new ShowPager(array('ct'=>$this->db($ct_db_name), 'select'=>$this->db($select_db_name)), $curr_page, $per_page);
    }
}


?>